package com.xialinrui.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xialinrui.entity.ResponseResult;
import com.xialinrui.entity.Summary;
import com.xialinrui.service.SummaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xialinrui
 */
@RestController
@Slf4j
@RequestMapping("/summary")
public class SummaryController {

    @Resource
    private SummaryService summaryService;

    @PostMapping("/add")
    public ResponseResult<Map<String, Long>> add(@RequestBody Summary summary) {
        QueryWrapper<Summary> summaryQueryWrapper = new QueryWrapper<>();
        summaryQueryWrapper.eq("summary_date",summary.getSummaryDate());
        Summary ifHas = summaryService.getOne(summaryQueryWrapper);
        if (ifHas != null){
            return new ResponseResult<>(200, "创建成功");
        }
        HashMap<String, Long> resMap = new HashMap<>(1);
        summaryService.add(summary);
        resMap.put("id", summary.getId());
        return new ResponseResult<>(200, "创建成功", resMap);
    }

    @GetMapping("/show")
    public ResponseResult<String> show(@RequestParam Long id) {
        Summary summary = summaryService.getById(id);
        return new ResponseResult<>(200, "查询成功", summary.getContent());
    }

    @GetMapping("/list")
    public ResponseResult<List<Summary>> list(@RequestParam Integer page, @RequestParam Integer pageSize) {
        Page<Summary> resPage = new Page<>(page, pageSize);
        QueryWrapper<Summary> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("summary_date");
        summaryService.page(resPage, queryWrapper);
        return new ResponseResult(200, "查询成功", resPage);
    }

    @PutMapping("/update")
    public ResponseResult update(@RequestParam Long id, @RequestParam String content) {
        UpdateWrapper<Summary> summaryUpdateWrapper = new UpdateWrapper<>();
        summaryUpdateWrapper.set("content", content).eq("id", id);
        summaryService.update(summaryUpdateWrapper);
        return new ResponseResult(200, "保存成功");
    }

    /**
     * 根据日期查询每日总结
     *
     * @param date
     * @return
     */
    @GetMapping("/by/date")
    public ResponseResult<Summary> byDate(@RequestParam String date) {
        QueryWrapper<Summary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("summary_date", date);
        Summary summary = summaryService.getOne(queryWrapper);
        return new ResponseResult<>(200, "查询成功", summary);
    }

    /**
     * 根据时间段查询每日总结
     * @param from
     * @param to
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/list/date")
    public ResponseResult<Page<Summary>> listDate(@RequestParam String from, @RequestParam String to, @RequestParam Integer page, @RequestParam Integer pageSize) {
        Page<Summary> resPage = new Page<>(page, pageSize);
        QueryWrapper<Summary> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("summary_date", from, to).orderByDesc("summary_date");
        summaryService.page(resPage, queryWrapper);
        return new ResponseResult(200, "查询成功", resPage);
    }

}
